﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DevDL.UserLimit.DBUtility;
using DevDL.UserLimit.IDAL;
using System.Data;
using DevDL.UserLimit.Models;
namespace DevDL.UserLimit.SQLServerDAL
{
    class OperatorDAL : IOperatorDAL
    {
        public Operator GetUserInfoByUserId(string userId)
        {
            Operator userInfo = null;
            string sql = "SELECT [id],[operatorName],[password],[RoleId],[state] FROM [operator] WHERE [operatorname]=@LoginId";
            SqlParameter par = new SqlParameter("@LoginId", userId);
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnSting(), CommandType.Text, sql, par))
                {
                    if (reader.Read())
                    {
                        userInfo = new Operator();
                        userInfo.Id =Convert.ToInt32( reader["id"].ToString());
                        userInfo.OperatorName = reader["operatorName"].ToString();
                        userInfo.Password = reader["password"].ToString();
                        userInfo.State = Convert.ToBoolean(reader["State"]);
                        userInfo.RoleId = Convert.ToInt32(reader["RoleId"]);
                    }
                    return userInfo;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public int Modify(Operator ope)
        {
            string sql = "UPDATE [Operator] SET RoleId=@RoleId WHERE [Id]=@Id";
            SqlParameter[] pars = {
                                  new SqlParameter("@RoleId",ope.RoleId),
                                  new SqlParameter("@Id",ope.Id)
                                  };
            return SqlHelper.ExecuteNonQuery(SqlHelper.GetConnection(), CommandType.Text, sql, pars);
        }
        public int Delete(int id)
        {
            string sql = "DELETE FROM [Operator] WHERE [Id]=@Id";
            SqlParameter par = new SqlParameter("@Id", id);
            return SqlHelper.ExecuteNonQuery(SqlHelper.GetConnection(), CommandType.Text, sql, par);
        }
        /// <summary>
        /// 添加操作者
        /// </summary>
        /// <param name="ope"></param>
        /// <returns></returns>
        public int Insert(Models.Operator ope)
        {
            string sql = "INSERT INTO [Operator](OperatorName,Password,RoleId,State)VALUES(@OperatorName,@Password,@RoleId,@State)";
            SqlParameter[] pars ={
                                    new SqlParameter("@OperatorName",ope.OperatorName),
                                    new SqlParameter("@Password",ope.Password),
                                    new SqlParameter("@RoleId",ope.RoleId),
                                    new SqlParameter("@State",ope.State)
                                };
            return SqlHelper.ExecuteNonQuery(SqlHelper.GetConnection(), CommandType.Text, sql, pars);
        }

        public List<Models.Operator> GetOperators(int id)
        {
            string condition = string.Format("[Id]={0}", id);
            return BaseMethod(condition);
        }
        public List<Operator> GetOperators()
        {
            return BaseMethod("");
        }
        /// <summary>
        /// 基础方法，获得操作员表的数据
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        private List<Operator> BaseMethod(string condition, params SqlParameter[] pars)
        {
            List<Models.Operator> list = new List<Operator>();
            string sql = "SELECT * FROM [Operator] WHERE 1=1";
            if (condition.Trim().Length != 0)
            {
                sql += " and ";
                sql += condition;
            }
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql, pars))
            {
                Operator o;
                while (reader.Read())
                {
                    o = new Operator();
                    o.Id = Convert.ToInt32(reader["Id"]);
                    o.OperatorName = reader["OperatorName"].ToString();
                    o.Password = reader["Password"].ToString();
                    o.RoleId =Convert.ToInt32(reader["RoleId"]);
                    o.State = Convert.ToBoolean(reader["State"]);
                    list.Add(o);
                }
                return list;
            }
        }
    }
}
